package com.aiti.lulian.controller;

import cn.hutool.core.util.IdUtil;
import com.aiti.base.core.base.controller.WebController;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.entity.CardRecordSheet;
import com.aiti.lulian.service.ICardRecordSheetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

@RestController
@RequestMapping("/cardRecordSheet")
@Api(tags = " cardRecordSheet", description = "")
public class CardRecordSheetController extends WebController {

    @Autowired
    private ICardRecordSheetService cardRecordSheetService;

    @ApiOperation("importSheet")
    @PostMapping("importSheet")
    public Message importSheet(@RequestParam("file") MultipartFile file){
        List<CardRecordSheet> list=new ArrayList<>();
        try {
        InputStream f=file.getInputStream();
        Workbook workbook = new XSSFWorkbook(f);
        // 获取第一个工作表（索引从0开始），你也可以根据实际情况选择其他工作表
        Sheet sheet = workbook.getSheetAt(0);
            SimpleDateFormat sdf = new SimpleDateFormat("dd-MMMM-yyyy", Locale.CHINA);
        // 从第二行开始遍历工作表中的行（索引从1开始，因为0代表第一行）
        for (int rowIndex = 1; rowIndex < sheet.getLastRowNum() + 1; rowIndex++) {
            CardRecordSheet cardRecordSheet=new CardRecordSheet();
            cardRecordSheet.setBaseId(IdUtil.simpleUUID());
            Row row = sheet.getRow(rowIndex);
            if (row!= null) {
                if(row.getCell(1)!=null){
                    String area=row.getCell(1).toString();
                    cardRecordSheet.setArea(area);
                }
                if(row.getCell(2)!=null){
                    String enterpriseName=row.getCell(2).toString();
                    cardRecordSheet.setEnterpriseName(enterpriseName);
                }
                if(row.getCell(3)!=null){
                    String linkMan=row.getCell(3).toString();
                    cardRecordSheet.setEnterpriseName(linkMan);
                }
                if(row.getCell(4)!=null){
                    String tel=phone(row.getCell(4));
                    cardRecordSheet.setTel(tel);
                }
                if(row.getCell(5)!=null){
                    String userName=phone(row.getCell(5));
                    cardRecordSheet.setUserName(userName);
                }
                if(row.getCell(6)!=null){
                    String orderFrom=row.getCell(6).toString();
                    cardRecordSheet.setOrderFrom(orderFrom);
                }
                if(row.getCell(7)!=null){
                    String orderTime =row.getCell(7).toString();
                    if(StringUtils.isNotBlank(orderTime)){
                        Date date =sdf.parse(orderTime);
                        cardRecordSheet.setOrderTime(date);
                    }
                }
                if(row.getCell(8)!=null){
                    String cycle=row.getCell(8).toString();
                    cardRecordSheet.setCycle(cycle);
                }
                if(row.getCell(9)!=null){
                    String status=row.getCell(9).toString();
                    if(status.contains("是")){
                        cardRecordSheet.setStatus("1");
                    }else {
                        cardRecordSheet.setStatus("0");
                    }
                }
                list.add(cardRecordSheet);
            }
        }
        cardRecordSheetService.saveBatch(list);
        } catch (IOException | ParseException e) {
            e.printStackTrace();
        }
        return Message.success();
    }

    private static String phone(Cell c){
        String tel="";
        if (DateUtil.isCellDateFormatted(c)) {
            tel =  c.getDateCellValue().toString();
        } else {
            BigDecimal bd = new BigDecimal(c.getNumericCellValue());
            tel = bd.toString();
        }
        return  tel;
    }
}
